<template>
  <view class="pt4 px-2 publish h-full">
    <wd-message-box/>
    <wd-toast/>
    <view class="form">
      <form @submit="formSubmit">
        <wd-cell-group custom-class="group" title="基础信息" border>
          <wd-input type="text" label="司机姓名" placeholder="请输入司机姓名" required v-model="formData.COL_0.value"/>
          <wd-input type="text" label="手机号码" placeholder="请输入手机号码" required v-model="formData.COL_1.value"/>
          <wd-input type="text" label="身份证号" placeholder="请输入身份证号" required v-model="formData.COL_2.value"/>
          <wd-picker required :columns="userInfo.xiangmuList" label="项目名称" v-model="formData.COL_3.value" value-key="COL_3" label-key="COL_3" @confirm="xiangmuChange"/>
        </wd-cell-group>
        <view class="text-center p3">
          <wd-button block type="success" @click="formSubmit">提交</wd-button>
          <view style="color:#444;font-size: 28rpx" class="text-center mt-4" @click="openPrivacyContract">
            《用户隐私保护指引》
          </view>
        </view>
      </form>
    </view>
  </view>
</template>

<script setup>
const {userInfo} = storeToRefs(useAuthStore())
import {useMessage} from 'wot-design-uni'
import {useToast} from 'wot-design-uni'
import DemoApi from "@/api/DemoApi";
const toast = useToast()
const messageBox = useMessage()
const formData = ref({
  COL_0: {
    "field": "COL_0",
    "fieldId": 365,
    "fieldName": "司机",
    "dataType": "STRING",
    "tableAliasName": "iPWTm",
    "tableName": "mdm_driver"
  },
  COL_1: {
    "field": "COL_1",
    "fieldId": 366,
    "fieldName": "手机号码",
    "dataType": "STRING",
    "tableAliasName": "iPWTm",
    "tableName": "mdm_driver"
  },
  COL_2: {
    "field": "COL_2",
    "fieldId": 367,
    "fieldName": "身份证号",
    "dataType": "STRING",
    "tableAliasName": "iPWTm",
    "tableName": "mdm_driver"
  },
  COL_3: {
    "field": "COL_3",
    "fieldId": 368,
    "fieldName": "负责项目名称",
    "dataType": "STRING",
    "tableAliasName": "iPWTm",
    "tableName": "mdm_driver"
  },
  COL_4: {
    "field": "COL_4",
    "fieldId": 369,
    "fieldName": "项目编号",
    "dataType": "STRING",
    "tableAliasName": "iPWTm",
    "tableName": "mdm_driver"
  },
  COL_5: {
    "field": "COL_5",
    "fieldId": 370,
    "fieldName": "队长名",
    "dataType": "STRING",
    "tableAliasName": "iPWTm",
    "tableName": "mdm_driver",
    value: userInfo.value.name
  },
  COL_6: {
    "field": "COL_6",
    "fieldId": 371,
    "fieldName": "队长手机号码",
    "dataType": "STRING",
    "tableAliasName": "iPWTm",
    "tableName": "mdm_driver",
    value: userInfo.value.COL_1
  }
})

let id = ''
onLoad((options) => {
  if (options.item) {
    const item = JSON.parse(decodeURIComponent(options.item))
    id = item.ID
    Object.values(formData.value).forEach((item2) => {
      item2.value = item[item2.field]
    })
  }
})

const xiangmuChange = (e) => {
  formData.value.COL_3.value = e.value
  formData.value.COL_4.value = e.selectedItems.COL_4
}
const formSubmit = () => {
  if (!formData.value.COL_0.value) {
    toast.error('请输入司机姓名')
    return
  }
  if (!formData.value.COL_1.value) {
    toast.error('请输入手机号码')
    return
  }
  if (!formData.value.COL_3.value) {
    toast.error('请选择项目名称')
    return
  }
  let params = {}
  Object.values(formData.value).forEach((item) => {
    let value = item.value
    params[item.fieldId] = value
  })
  params = JSON.stringify(params)
  messageBox
      .confirm({
        title: '提示',
        msg: '确认提交吗？'
      })
      .then(() => {
        toast.loading({loadingType: 'ring', msg: '提交中'})
        let url = 'https://miniapp.zhangmushiye.com/visual-api/paas/visualdev/OnlineDev/289135d883f74b176a1045620f95877e'
        if (id) {
          url = url + '/' + id
          // PUT 提交
          uni.request({
            url,
            method: 'PUT',
            data: {
              data: params,
              id
            },
            success: (res) => {
              toast.success('提交成功')
              uni.navigateBack()
            },
            fail: (err) => {
              toast.error('提交失败')
            }
          })
        }else{
          uni.request({
            url,
            method: 'POST',
            data: {
              data: params,
            },
            success: (res) => {
              toast.success('提交成功')
              uni.navigateBack()
            },
            fail: (err) => {
              toast.error('提交失败')
            }
          })
        }

      })
      .catch((error) => {
        console.log(error)
      })
}

const openPrivacyContract = () => {
  wx.openPrivacyContract({
    success: (res) => {
      isAuth.value = true
    },
    fail: (err) => {
      isAuth.value = false
    }
  })
}
</script>

<style scoped lang="scss">

</style>
